package com.leyou.item.controller;

import com.leyou.common.enums.LyExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.pojo.Item;
import com.leyou.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    /**
     * 保存商品
     */
    @PostMapping("save")
    public ResponseEntity<Item> save(Item item){
        //判断价格是否为空
        if(item.getPrice()==null){
           // return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            throw new LyException(LyExceptionEnum.PRICE_NOT_NULL);
        }

        Item resultItem = itemService.save(item);
        return ResponseEntity.status(HttpStatus.CREATED).body(resultItem);
    }

}
